//#define PURPLE_PLUGINS
#include "account.h"
#include "glib.h"
#include "log.h"
#include "notify.h"
#include "plugin.h"
#include "version.h"
#include "conversation.h"
#include "internal.h"
#include "pidgin.h"

#include "debug.h"

#include "prefs.h"
#include "signals.h"
#include "util.h"
#include "version.h"
#include "string.h"
#include "gtkconv.h"
#include "gtkimhtml.h"
#include "gtkplugin.h"

#define INTERVAL 100
#define CLIENT2MSNGR "/tmp/c2m"
#define MSNGR2CLIENT "/tmp/m2c"
#define DATA_PACKET 100

guint g_tid;
PurpleConversation *globalconv;
int fd_c2m, fd_m2c;
char *our_link;
int ourmsg = FALSE;

void format (char *);

/* Extracting commands from message */
void
format(char *type) {
  char *pt1, *pt2, *temp;
  int diff;
  pt1 = strstr (our_link, type);
  pt2 = strstr (our_link, "</body>");
  diff = pt2 - pt1 - 7;
  temp = pt1 + 7;
  
  temp[diff] = '\0';
  our_link = temp;
}

static gboolean
cb_pidssh () {
  int i;
  char c[DATA_PACKET];
  
  if ((i = read (fd_c2m, c, DATA_PACKET)) > 0) {
    ourmsg = TRUE;
    purple_conv_im_send (PIDGIN_CONVERSATION (globalconv), c);	
  }
  else
    perror ("\nError in reading pipe : ");
  return TRUE;
}

/* Check for IM or command. */
static gboolean 
receiving_msg_cb (PurpleAccount *account, char **sender,
		  char **message, PurpleConversation *conv, int *flags) {
  char *check,*type;

  our_link = g_strdup (*message);
  check = strstr (our_link, "prdssh:");
  if (check != NULL) {
    type = "prdssh:";
    format (type);
    write (fd_m2c, our_link, 170);
    (*flags) = 0x8000;
    return TRUE;
  }
  else 
    return FALSE;
}


static void 
cb_writebuff (PurpleConversation *conv) {
  globalconv = conv;
  if ((fd_c2m = open("CLIENT2MSNGR",O_RDONLY|O_NONBLOCK)) == -1)
    printf ("\n(PidginSSHClient) error in opening fifo c2m\n");
  if ((fd_m2c = open("MSNGR2CLIENT",O_WRONLY)) < 0)
    printf ("\n(PidginSSHClient) Error in opening fifo m2c\n");
  g_tid = purple_timeout_add (INTERVAL, &cb_pidssh, 0);
}

static void 
cb_stop() {
  close (fd_c2m);
  close (fd_m2c);
  purple_timeout_remove (g_tid);
}

static gboolean 
hide_msg (PurpleAccount *account, const char *who, char **message,
	  PurpleConversation *conv, PurpleMessageFlags flags) {
  if (ourmsg == TRUE) {
    ourmsg = FALSE;
    return TRUE;
  }
  else
    return FALSE;
}


static gboolean 
plugin_load (PurplePlugin *plugin) {  

  void *conv_handle = purple_conversations_get_handle ();
  
  purple_signal_connect (conv_handle, "conversation-created", plugin,
			 PURPLE_CALLBACK (cb_writebuff), NULL);
  purple_signal_connect (conv_handle, "deleting-conversation", plugin,
			 PURPLE_CALLBACK (cb_stop), NULL);
  purple_signal_connect (conv_handle, "writing-im-msg",	plugin,
			 PURPLE_CALLBACK (hide_msg), NULL);	
  purple_signal_connect (conv_handle,"receiving-im-msg", plugin,
			 PURPLE_CALLBACK (receiving_msg_cb),NULL);
  return TRUE;
}


static PurplePluginInfo info = {
  PURPLE_PLUGIN_MAGIC,
  PURPLE_MAJOR_VERSION,
  PURPLE_MINOR_VERSION,
  PURPLE_PLUGIN_STANDARD, 
  NULL,
  0,
  NULL,
  PURPLE_PRIORITY_DEFAULT,
  
  "core-PidginSSHClient",
  "PidginSSHClient",
  "1.1",
  
  "PidginSSHClient Plugin",          
  "PidginSSHClient Plugin",          
  "Shoba Ramaseshan <shoba.ramaseshan@gmail.com>",                          
  "http://code.google.com/p/pidgin-ssh",     
  
  plugin_load,   
  NULL,              
                     
  NULL,                          
                                 
  NULL,                          
  NULL,                          
  NULL,                        
  NULL,                   
  NULL,                          
  NULL,                          
  NULL,                          
  NULL                           
};                               
    
static void                        
init_plugin(PurplePlugin *plugin) {    

}

PURPLE_INIT_PLUGIN(PidginSSHClient, init_plugin, info)
